package com.web_bloks.document.client;

//import com.web_bloks.system.Logger;

import java.lang.String;
import com.google.gwt.core.client.GWT;

import com.web_bloks.types.client.SimpleMap;

/**
 * Namespaces
 *  map: id 	=> name
 *       key	=> value
 *  
 * @author Jean-Lou Dupont
 *
 */
public class Namespaces {

	final public static String thisClass = "com.web_bloks.document.client.Namespaces";
	
	static SimpleMap base = null;
	static SimpleMap raw  = null;
	
	/*===================================================================
	 * CONSTRUCTORS
	 ===================================================================*/

	protected Namespaces() {
		super();
		if (null==base || null==raw) {
			base = (SimpleMap) GWT.create( SimpleMap.class );
			raw  = (SimpleMap) GWT.create( SimpleMap.class );
			init();			
		}
	}
	/**
	 * Initializes the default namespaces
	 * 
	 */
	protected void init() {
		DefaultNamespaces defns = (DefaultNamespaces) GWT.create(DefaultNamespaces.class);
		
		// main
		add( defns.domain1(), defns.main_base(), defns.main_raw() );
		
		// template
		add( defns.domain2(), defns.template_base(), defns.template_raw() );
		
	}
	
	/*===================================================================
	 * BUILDERS
	 ===================================================================*/
	
	
	/*===================================================================
	 * API for SUB-CLASSES
	 ===================================================================*/
	/**
	 * Adds a namespace to the Namespaces
	 *  Only to be used by sub-classes
	 */
	protected void add(String domainId, String base, String raw) {
		Namespaces.base.setKey( domainId, base );
		Namespaces.raw.setKey( domainId, raw );
	}
	protected String lookupInBase(String name) {
		//Logger.logInfo(this.getClass()+".lookupInBase: name["+name+"]");
		
		String[] pair;
		
		base.reset();
		while(base.hasNext()){
			pair = base.nextStringPair();
			//Logger.logInfo(this.getClass()+".lookupInBase: pair0["+pair[0]+"] pair1["+pair[1]+"]");			
			//if (pair[1].length() == 0 && name.length() == 0)
			//	return new String( pair[0] );
			if (pair[1].toLowerCase().compareTo(name) == 0) {
				//Logger.logInfo(thisClass+".lookupInBase: FOUND name["+name+"]");				
				return new String( pair[0] );
			}
		}
		
		return null;
	}
	protected String lookupInRaw(String name) {
		//Logger.logInfo(this.getClass()+".lookupInRaw: name["+name+"]");	
		
		String[] pair;
		
		raw.reset();
		while(raw.hasNext()){
			pair = raw.nextStringPair();
			//Logger.logInfo(this.getClass()+".lookupInRaw: pair0["+pair[0]+"] pair1["+pair[1]+"]");
			
			//if (pair[1].length() == 0 && name.length() == 0)
			//	return new String( pair[0] );
			if (pair[1].toLowerCase().compareTo(name) == 0) {
				//Logger.logInfo(this.getClass()+".lookupInRaw: FOUND name["+name+"]");
				return new String( pair[0] );
			}
			
		}
		
		return null;
	}
	/*===================================================================
	 * API
	 ===================================================================*/
	/**
	 * Looks-up the 'domain id' for a given 'name'
	 * 
	 * @param name String
	 * @return id String
	 */
	public String lookup(String name) {
		//Logger.logInfo(this.getClass()+".lookup: name["+name+"]");
		if ( null == name )
			return null;

		String id = null;
		
		if ( (id=lookupInBase(name)) != null )
			return id;
		
		if ( (id=lookupInRaw(name)) != null )
			return id;
		
		return null;
	}
	
	public boolean isRaw(String name) {
		if (name == null)
			return false;
		
		return (lookupInRaw(name) != null);
	}

	public boolean isBase(String name) {
		if (name == null)
			return false;
		
		return (lookupInBase(name) != null);
	}
	
	public String getBaseFromDomainId(String id) {
		//Logger.logInfo(this.getClass()+".getBaseFromDomainId: id["+id+"]");
		
		if (null == id)
			return null;
		
		return base.getKey(id);
	}
	public String getRawFromDomainId(String id) {
		//Logger.logInfo(this.getClass()+".getRawFromDomainId: id["+id+"]");		
		if (null == id)
			return null;
		
		return raw.getKey(id);
	}
	
	
	/*===================================================================
	 * BUILDERS 
	 ===================================================================*/
	/**
	 * Creates a corresponding 'raw' namespace from a 'base' one
	 * 
	 * @param base namespace
	 * @return raw namespace
	 */
	public Namespace createRaw(Namespace ns) throws NamespaceException {

		if (null == ns)
			return null;
		
		String domainid = lookup(ns.getName());
		if ( null == domainid ) {
			throw new NamespaceException(thisClass+".createRaw: can't find domain");
		}
		
		String name = getRawFromDomainId(domainid);
		
		Namespace result = GWT.create(Namespace.class);
		
		result.setDomainId( domainid );
		result.setName( name );
		
		return result;
	}

	public Namespace createBase(Namespace ns) throws NamespaceException {

		if (null == ns)
			return null;
		
		String domainid = lookup(ns.getName());
		
		if ( null == domainid ) {
			throw new NamespaceException(thisClass+".createBase: can't find domain");
		}
		
		String name = getBaseFromDomainId(domainid);
		
		Namespace result = GWT.create(Namespace.class);
		
		result.setDomainId( domainid );
		result.setName( name );
		
		return result;
	}

}//END